package airthmetic.exercise.arr;

import java.util.Arrays;

public class _LCP18_早餐组合 {

    public static int breakfastNumber(int[] staple, int[] drinks, int x) {
        // 定义双指针 与 返回数量
        int num = 0;
        int p1 = 0;
        int p2 = drinks.length - 1;

        // 数组排序
        Arrays.sort(staple);
        Arrays.sort(drinks);

        // 如果p2当前位置金额 + p1当前位置金额 已经小于等于x 那p2向前左走的元素必然也是满足小于等x 所以直接拿元素个数(p2 + 1)
        while (p1 < staple.length && p2 >=0){
            if (staple[p1] + drinks[p2] <= x){
                num = (num + p2 + 1) % 1000000007;
                p1++;
            }else{
                p2--;
            }
        }

        return num;
    }

    public static void main(String[] args) {
        int[] staple = new int[]{10,20,5};
        int[] drinks = new int[]{5,5,2};
        int x = 15;
        System.out.println(breakfastNumber(staple, drinks, x));
    }
}
